import JQUERY from './jquery-1.12.4.min.js'
import WOW from './wow.min.js'

window.$ = JQUERY
window.wow = {}
// es6 IE测试66
let test = [1, 2, 3]
console.log([...test])
console.log(process.env.NODE_ENV)

// 跨域测试
window.testFun = () => {
    let api = '/cors/v2/d', params = {d: 'a', v: 'a', cb: '_WM_'};
    testGet(api, params, res => {
        console.log(res)
    })
}

// 页面效果
$(document).ready(function () {
    // 头部菜单
    var istop = true;
    $(window).scroll(function () {
        if ($('.head-md').css('display') === 'none') return;
        if ($(document).scrollTop() === 0) {
            showMenus();
        } else {

            if (istop) {
                $('.head-md').stop().animate({
                    width: "80px"
                }, 600);
                istop = false;
            }

            $('.head-md').addClass("fixed-header");
            $("#mainav").fadeOut();
        }


        if ($(document).scrollTop() > 768) {
            $("#totop").fadeIn();
        } else {
            $("#totop").fadeOut();
        }
    });

    $('.head-md a.logo').click(function () {
        showMenus();
    });

    function showMenus() {
        $('.head-md').stop().animate({
            width: "90%"
        }, 600);
        $('.head-md').removeClass("fixed-header");
        setTimeout(function () {
            $("#mainav").fadeIn();
        }, 600);
        istop = true;
    }

    //左上角小菜单
    $(".menu-toggle").on('click', function () {
        $(this).toggleClass("on");
        $('.site-header').toggleClass("openHeader");
        $(".sitenav").toggleClass('hidden');
    });
    //wowJsInit
    wow = new WOW({
        animateClass: 'animated',
        offset: 100,
        callback: function (box) {
            console.log("WOW: animating <" + box.tagName.toLowerCase() + ">");
        }
    });
    wow.init();
});

// ajax
function testGet(api, params, callback) {
    console.log(arguments)
    if (params.length <= 0) {
        return;
    }
    let xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.open("POST", api, true);
    // xmlhttp.open("GET", `${api}?wd=${params}`, true);
    xmlhttp.setRequestHeader( "Content-Type" , "application/json" );
    xmlhttp.onreadystatechange = _ => {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            callback && callback(xmlhttp.responseText);
        }
    }
    xmlhttp.send(JSON.stringify(params));
}